﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Perovich.GameObjects.Components
{
    public abstract class Factory
    {
        protected static ConstructorInfo GetBestMatchingConstructor(Type type, object[] parameters)
        {
            ConstructorInfo[] constructors = type.GetConstructors();

            // find the signatures that has the same amount of parameters, as the parameters passed
            List<ConstructorInfo> countMatches = new List<ConstructorInfo>();

            for (int i = 0; i < constructors.Length; i++)
            {
                if (constructors[i].GetParameters().Length == parameters.Length)
                {
                    countMatches.Add(constructors[i]);
                }
            }

            ConstructorInfo bestMatch = constructors[0];

            // find the best matching signature (the best match can still be an invalid signature)
            if (countMatches.Count > 0)
            {
                int indexOfBest = -1, currentBestMatches = -1;

                for (int i = 0; i < countMatches.Count; i++)
                {
                    ParameterInfo[] pi = countMatches[i].GetParameters();

                    int n = 0;

                    for (int j = 0; j < pi.Length; j++)
                    {
                        if (pi[j].ParameterType.Equals(parameters[j].GetType()))
                        {
                            n++;
                        }
                    }

                    if (n > currentBestMatches)
                    {
                        currentBestMatches = n;

                        indexOfBest = i;
                    }
                }

                bestMatch = countMatches[indexOfBest];
            }

            return bestMatch;
        }
    }
}
